//
//  BindViewController.m
//  Cat
//
//  Created by lushitong on 2019/1/9.
//  Copyright © 2019年 lushitong. All rights reserved.
//

#import "BindViewController.h"
#import "HttpUserManager.h"

@interface BindViewController ()<UITextFieldDelegate>

@property (nonatomic, strong) UILabel *accountLabel;
@property (nonatomic, strong) UITextField *accountFiled;
@property (nonatomic, strong) UILabel *nameLabel;
@property (nonatomic, strong) UITextField *nameFiled;
@property (nonatomic, strong) UILabel *phoneLabel;
@property (nonatomic, strong) UITextField *phoneTextFiled;
@property (nonatomic, strong) UILabel *codeLabel;
@property (nonatomic, strong) UITextField *codeTextFiled;
@property (nonatomic, strong) UIButton *bindButton;

@property (nonatomic, strong) UIButton *sendVerButton;

@property (nonatomic, strong) UIView *line1;
@property (nonatomic, strong) UIView *line2;
@property (nonatomic, strong) UIView *line3;
@property (nonatomic, strong) UIView *line4;

@end

@implementation BindViewController

- (void)viewDidLoad {
    [super viewDidLoad];
	[self.topTitleView setDetailDic:@{Nav_Title:@"绑定支付宝账号"}.mutableCopy];
	self.view.backgroundColor = [UIColor whiteColor];
	[self addAllSubViews];
	
}

- (void)addAllSubViews {
	[self.view addSubviews:@[self.accountLabel,self.accountFiled,self.nameLabel,self.nameFiled,self.phoneLabel,self.sendVerButton,self.phoneTextFiled,self.codeLabel,self.codeTextFiled,self.bindButton]];
	[self.view addSubviews:@[self.line1,self.line2,self.line3,self.line4]];
	
	[self.accountLabel mas_makeConstraints:^(MASConstraintMaker *make) {
		make.left.equalTo(self.view.mas_left).offset(kQKFitModuleDecimal(28.0));
		make.top.equalTo(self.topTitleView.mas_bottom).offset(30);
	}];
	
	[self.accountFiled mas_makeConstraints:^(MASConstraintMaker *make) {
		make.left.equalTo(self.view.mas_left).offset(kQKFitModuleDecimal(114));
		make.centerY.equalTo(self.accountLabel.mas_centerY);
		make.right.equalTo(self.view.mas_right).offset(-kQKFitModuleDecimal(23));
		make.height.equalTo(@(kQKFitModuleDecimal(25)));
	}];
	
	[self.line1 mas_makeConstraints:^(MASConstraintMaker *make) {
		make.left.equalTo(self.view.mas_left).offset(kQKFitModuleDecimal(23));
		make.right.equalTo(self.view.mas_right).offset(-kQKFitModuleDecimal(23));
		make.height.equalTo(@(kQKFitModuleDecimal(1.0)));
		make.top.equalTo(self.accountLabel.mas_bottom).offset(kQKFitModuleDecimal(5.0));
	}];
	
	[self.nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
		make.left.equalTo(self.accountLabel.mas_left);
		make.top.equalTo(self.line1.mas_bottom).offset(kQKFitModuleDecimal(37.0));
	}];
	
	[self.nameFiled mas_makeConstraints:^(MASConstraintMaker *make) {
		make.left.equalTo(self.view.mas_left).offset(kQKFitModuleDecimal(66));
		make.centerY.equalTo(self.nameLabel.mas_centerY);
		make.right.equalTo(self.view.mas_right).offset(-kQKFitModuleDecimal(23));
		make.height.equalTo(@(kQKFitModuleDecimal(25)));
	}];
	
	[self.line2 mas_makeConstraints:^(MASConstraintMaker *make) {
		make.left.equalTo(self.view.mas_left).offset(kQKFitModuleDecimal(23));
		make.right.equalTo(self.view.mas_right).offset(-kQKFitModuleDecimal(23));
		make.height.equalTo(@(kQKFitModuleDecimal(1.0)));
		make.top.equalTo(self.nameLabel.mas_bottom).offset(kQKFitModuleDecimal(5.0));
	}];
	
	[self.phoneLabel mas_makeConstraints:^(MASConstraintMaker *make) {
		make.left.equalTo(self.accountLabel.mas_left);
		make.width.equalTo(@(kQKFitModuleDecimal(80)));
		make.top.equalTo(self.line2.mas_bottom).offset(kQKFitModuleDecimal(37.0));
	}];
	
	[self.phoneTextFiled mas_makeConstraints:^(MASConstraintMaker *make) {
		make.left.equalTo(self.phoneLabel.mas_right).offset(kQKFitModuleDecimal(8));
		make.centerY.equalTo(self.phoneLabel.mas_centerY);
		make.right.equalTo(self.view.mas_right).offset(-kQKFitModuleDecimal(23));
		make.height.equalTo(@(kQKFitModuleDecimal(25)));
	}];
	
	[self.line3 mas_makeConstraints:^(MASConstraintMaker *make) {
		make.left.equalTo(self.view.mas_left).offset(kQKFitModuleDecimal(23));
		make.right.equalTo(self.view.mas_right).offset(-kQKFitModuleDecimal(23));
		make.height.equalTo(@(kQKFitModuleDecimal(1.0)));
		make.top.equalTo(self.phoneLabel.mas_bottom).offset(kQKFitModuleDecimal(5.0));
	}];
	
	[self.codeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
		make.left.equalTo(self.phoneLabel.mas_left);
		make.top.equalTo(self.line3.mas_bottom).offset(kQKFitModuleDecimal(37.0));
		make.right.equalTo(self.phoneLabel.mas_right);
	}];
	
	[self.codeTextFiled mas_makeConstraints:^(MASConstraintMaker *make) {
		make.left.equalTo(self.codeLabel.mas_right).offset(kQKFitModuleDecimal(8));
		make.centerY.equalTo(self.codeLabel.mas_centerY);
		make.right.equalTo(self.view.mas_right).offset(-kQKFitModuleDecimal(80 + 27));
		make.height.equalTo(@(kQKFitModuleDecimal(25)));
	}];
	
	[self.sendVerButton mas_makeConstraints:^(MASConstraintMaker *make) {
		make.centerY.equalTo(self.codeTextFiled.mas_centerY).offset(-kQKFitModuleDecimal(2.0));
		make.right.equalTo(self.view.mas_right).offset(-kQKFitModuleDecimal(27.0));
		make.size.equalTo(CGSizeMake(kQKFitModuleDecimal(80), kQKFitModuleDecimal(28)));
	}];
	
	[self.line4 mas_makeConstraints:^(MASConstraintMaker *make) {
		make.left.equalTo(self.view.mas_left).offset(kQKFitModuleDecimal(23));
		make.right.equalTo(self.view.mas_right).offset(-kQKFitModuleDecimal(23));
		make.height.equalTo(@(kQKFitModuleDecimal(1.0)));
		make.top.equalTo(self.codeLabel.mas_bottom).offset(kQKFitModuleDecimal(5.0));
	}];
	
	[self.bindButton mas_makeConstraints:^(MASConstraintMaker *make) {
		make.size.equalTo(CGSizeMake(kQKFitModuleDecimal(321), kQKFitModuleDecimal(40)));
		make.centerX.equalTo(self.view.mas_centerX);
		make.top.equalTo(self.line4.mas_bottom).offset(36);
	}];
	
	[self setPlaceholder:@"您用于提现的支付宝账号" andWith:self.accountFiled];
	[self setPlaceholder:@"支付宝实名认证姓名" andWith:self.nameFiled];
}

- (void)setPlaceholder:(NSString *)placeholder andWith:(UITextField *)textFiled {
	NSDictionary *atts = @{NSForegroundColorAttributeName: COLOR_WITH_HEX(0xCDC7C4),
						   NSFontAttributeName: textFiled.font};
	NSAttributedString *attStr = [[NSAttributedString alloc] initWithString:placeholder attributes:atts];
	textFiled.attributedPlaceholder = attStr;
}

- (void)textFieldValueChanged:(UITextField *)textField {
	if (self.codeTextFiled.text.length >0 && self.accountFiled.text.length > 0 && self.nameFiled.text.length > 0 && self.phoneTextFiled.text.length >0) {
		self.bindButton.selected = YES;
	} else {
		self.bindButton.selected = NO;
	}
	if (self.phoneTextFiled.text.length < 11) {
		self.sendVerButton.selected = NO;
	} else {
		self.sendVerButton.selected = YES;
	}
}

- (void)bindActionWithButton:(UIButton *)button {
	if (self.codeTextFiled.text.length >0 && self.accountFiled.text.length > 0 && self.nameFiled.text.length > 0 && self.phoneTextFiled.text.length >0) {
		[HttpUserManager balanceUpdatePayName:self.accountFiled.text realName:self.nameFiled.text phoneNumber:self.phoneTextFiled.text verCode:self.codeTextFiled.text withSuccuss:^(NSDictionary *dict) {
			[FLProgressHUD showSuccess:@"绑定成功"];
			[self.navigationController popViewControllerAnimated:YES];
		} failure:^(NSInteger code, NSString *message) {
			[FLProgressHUD showError:message];
		}];
	} else {
		[FLProgressHUD showSuccess:@"请填写完整信息"];
		
	}
	
}

- (void)senVerCodeWithButton:(UIButton *)button {
	if (self.phoneTextFiled.text.length < 11) {
		[FLProgressHUD showError:@"请输入正确的验证码"];
		return;
	}
	[HttpUserManager sendVerifyCodeWithMobile:self.phoneTextFiled.text withSuccuss:^(NSDictionary *dict) {
		[self openCountdown:button];
		[FLProgressHUD showSuccess:@"发送成功"];
	} failure:^(NSInteger code, NSString *message) {
		[FLProgressHUD showError:message];
	}];
}

- (void)openCountdown:(UIButton *)button {
	__block NSInteger time = 59; //倒计时时间
	
	dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
	dispatch_source_t _timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queue);
	
	dispatch_source_set_timer(_timer,dispatch_walltime(NULL, 0),1.0 * NSEC_PER_SEC, 0); //每秒执行
	
	dispatch_source_set_event_handler(_timer, ^{
		if(time <= 0){ //倒计时结束，关闭
			dispatch_source_cancel(_timer);
			dispatch_async(dispatch_get_main_queue(), ^{
				//设置按钮的样式
				[button setTitle:@"重发验证码" forState:UIControlStateNormal];
				button.selected = YES;
				button.userInteractionEnabled = YES;
			});
			
		}else{
			int seconds = time % 60;
			dispatch_async(dispatch_get_main_queue(), ^{
				//设置按钮显示读秒效果
				[button setTitle:[NSString stringWithFormat:@"%.2ds", seconds] forState:UIControlStateNormal];
				button.selected = NO;
				button.userInteractionEnabled = NO;
			});
			time--;
		}
	});
	dispatch_resume(_timer);
}

#pragma mark----- lazy init -----

- (UILabel *)accountLabel {
	if (!_accountLabel) {
		_accountLabel = [UILabel labelWithText:@"支付宝账号" font:[UIFont boldSystemFontOfSize:kQKFitFont(16)] textColor:COLOR_WITH_HEX(0x4c4947)];
	}
	return _accountLabel;
}

- (UITextField *)accountFiled {
	if (!_accountFiled) {
		_accountFiled = [[UITextField alloc] init];
		_accountFiled.font = [UIFont systemFontOfSize:kQKFitModule(14)];
		_accountFiled.textColor = COLOR_WITH_HEX(0xCDC7C4);
		_accountFiled.tintColor = COLOR_WITH_HEX(0xFFC236);
		_accountFiled.delegate = self;
		[_accountFiled addTarget:self action:@selector(textFieldValueChanged:) forControlEvents:UIControlEventEditingChanged];
	}
	return _accountFiled;
}

- (UILabel *)nameLabel {
	if (!_nameLabel) {
		_nameLabel = [UILabel labelWithText:@"姓名" font:[UIFont boldSystemFontOfSize:kQKFitFont(14)] textColor:COLOR_WITH_HEX(0x4c4947)];
	}
	return _nameLabel;
}

- (UITextField *)nameFiled {
	if (!_nameFiled) {
		_nameFiled = [[UITextField alloc] init];
		_nameFiled.font = [UIFont systemFontOfSize:kQKFitModule(14)];
		_nameFiled.textColor = COLOR_WITH_HEX(0xCDC7C4);
		_nameFiled.tintColor = COLOR_WITH_HEX(0xFFC236);
		_nameFiled.delegate = self;
		[_nameFiled addTarget:self action:@selector(textFieldValueChanged:) forControlEvents:UIControlEventEditingChanged];
	}
	return _nameFiled;
}

- (UILabel *)phoneLabel {
	if (!_phoneLabel) {
		_phoneLabel = [UILabel labelWithText:@"手机号" font:[UIFont systemFontOfSize:kQKFitFont(16)] textColor:COLOR_WITH_HEX(0x4c4947)];
	}
	return _phoneLabel;
}

- (UITextField *)phoneTextFiled {
	if (!_phoneTextFiled) {
		_phoneTextFiled = [[UITextField alloc] init];
		_phoneTextFiled.font = [UIFont systemFontOfSize:kQKFitModule(14)];
		_phoneTextFiled.textColor = COLOR_WITH_HEX(0xCDC7C4);
		_phoneTextFiled.tintColor = COLOR_WITH_HEX(0xFFC236);
		_phoneTextFiled.delegate = self;
		[_phoneTextFiled addTarget:self action:@selector(textFieldValueChanged:) forControlEvents:UIControlEventEditingChanged];
	}
	return _phoneTextFiled;
}

- (UILabel *)codeLabel {
	if (!_codeLabel) {
		_codeLabel = [UILabel labelWithText:@"验证码" font:[UIFont systemFontOfSize:kQKFitFont(16)] textColor:COLOR_WITH_HEX(0x4c4947)];
	}
	return _codeLabel;
}


- (UIButton *)sendVerButton {
	if (!_sendVerButton) {
		_sendVerButton = [UIButton buttonWithType:UIButtonTypeCustom];
		UIImage *nmImage = [UIImage imageWithColor:COLOR_WITH_HEX(0xFFC236)];
		UIImage *hlImage = [nmImage addBlackMask];
		UIImage *disImage = [UIImage imageWithColor:COLOR_WITH_HEX(0xCDC7C4)];
		
		[_sendVerButton setTitle:@"验证码" forState:UIControlStateNormal];
		_sendVerButton.titleLabel.font = [UIFont systemFontOfSize:kQKFitFont(12)];
		
		
		[_sendVerButton setBackgroundImage:nmImage forState:UIControlStateNormal];
		[_sendVerButton setBackgroundImage:hlImage forState:UIControlStateSelected];
		[_sendVerButton setBackgroundImage:disImage forState:UIControlStateDisabled];
		
		[_sendVerButton setTitleColor:COLOR_WITH_HEX(0x666666) forState:UIControlStateNormal];
		[_sendVerButton setTitleColor:COLOR_WITH_HEX(0x666666) forState:UIControlStateSelected];
		_sendVerButton.layer.cornerRadius = kQKFitModule(2);
		_sendVerButton.layer.masksToBounds = YES;
		_sendVerButton.selected = NO;
		[_sendVerButton addTarget:self action:@selector(senVerCodeWithButton:) forControlEvents:UIControlEventTouchUpInside];
	}
	return _sendVerButton;
}

- (UITextField *)codeTextFiled {
	if (!_codeTextFiled) {
		_codeTextFiled = [[UITextField alloc] init];
		_codeTextFiled.font = [UIFont systemFontOfSize:kQKFitModule(14)];
		_codeTextFiled.textColor = COLOR_WITH_HEX(0xCDC7C4);
		_codeTextFiled.tintColor = COLOR_WITH_HEX(0xFFC236);
		_codeTextFiled.delegate = self;
		[_codeTextFiled addTarget:self action:@selector(textFieldValueChanged:) forControlEvents:UIControlEventEditingChanged];
	}
	return _codeTextFiled;
}

- (UIButton *)bindButton {
	if (!_bindButton) {
		UIImage *nmImage = [UIImage imageWithColor:COLOR_WITH_HEX(0xFFC236)];
		UIImage *hlImage = [nmImage addBlackMask];
		UIImage *disImage = [UIImage imageWithColor:COLOR_WITH_HEX(0xCDC7C4)];
		
		_bindButton = [UIButton buttonWithType:UIButtonTypeCustom];
		[_bindButton setTitle:@"立即绑定" forState:UIControlStateNormal];
		
		[_bindButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
		[_bindButton setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];
		[_bindButton setTitleColor:[UIColor whiteColor] forState:UIControlStateDisabled];
		
		[_bindButton setBackgroundImage:nmImage forState:UIControlStateNormal];
		[_bindButton setBackgroundImage:hlImage forState:UIControlStateSelected];
		[_bindButton setBackgroundImage:disImage forState:UIControlStateDisabled];
		[_bindButton addTarget:self action:@selector(bindActionWithButton:) forControlEvents:UIControlEventTouchUpInside];
		_bindButton.layer.cornerRadius = kQKFitModule(5);
		_bindButton.layer.masksToBounds = YES;
		_bindButton.enabled = YES;
	}
	return _bindButton;
}
- (UIView *)line1 {
	if (!_line1) {
		_line1 = [[UIView alloc] init];
		_line1.backgroundColor = COLOR_WITH_HEX(0xEEEBEB);
	}
	return _line1;
}

- (UIView *)line2 {
	if (!_line2) {
		_line2 = [[UIView alloc] init];
		_line2.backgroundColor = COLOR_WITH_HEX(0xEEEBEB);
	}
	return _line2;
}


- (UIView *)line3 {
	if (!_line3) {
		_line3 = [[UIView alloc] init];
		_line3.backgroundColor = COLOR_WITH_HEX(0xEEEBEB);
	}
	return _line3;
}

- (UIView *)line4 {
	if (!_line4) {
		_line4 = [[UIView alloc] init];
		_line4.backgroundColor = COLOR_WITH_HEX(0xEEEBEB);
	}
	return _line4;
}
@end
